package com.ruoyi.web.wrapper;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.channel.enums.ChannelTypeEnum;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.common.wrapper.BaseControllerWrapper;
import com.ruoyi.db.domain.ProductBaseInfo;
import com.ruoyi.product.service.IProductBaseInfoService;
import com.ruoyi.product.service.enums.WarehouseTypeEnum;

import java.util.List;
import java.util.Map;

public class ProductOnlieInfoWrapper extends BaseControllerWrapper {

    public ProductOnlieInfoWrapper(Map<String, Object> single) {
        super(single);
    }

    public ProductOnlieInfoWrapper(List<Map<String, Object>> multi) {
        super(multi);
    }

    @Override
    public void wrapTheMap(Map<String, Object> map) {

        try {
            //logo
            String productId = map.get("productId").toString();
            long l = Long.parseLong(productId);
            IProductBaseInfoService iProductBaseInfoService = SpringUtils.getBean(IProductBaseInfoService.class);
            ProductBaseInfo productBaseInfo = iProductBaseInfoService.selectProductBaseInfoById(l);
            if (productBaseInfo != null){
                map.put("logoUrl",productBaseInfo.getLogoUrl());
            }
            //数据转化
            String channelType = (String) map.get("channelType");
            if (StrUtil.isNotEmpty(channelType)){
                if (channelType.contains(",")) {
                    String substring = channelType.substring(0, channelType.length());
                    String[] result = substring.split(",");
                    StringBuilder stringBuilder = new StringBuilder();
                    for (String s : result) {
                        if (StringUtils.isNotEmpty(s)) {
                            String key = ChannelTypeEnum.getValueByName(s);
                            stringBuilder.append(key).append(",");
                        }
                    }
                    String key = stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString();
                    map.put("channelTypeStr", key);
                } else {
                    String key = ChannelTypeEnum.getValueByName(channelType);
                    map.put("channelTypeStr", key);
                }
//                map.put("channelTypeStr", ChannelTypeEnum.getValueByName(channelType));
            }
            String sceneIds = (String) map.get("sceneId");
            if (StrUtil.isNotEmpty(sceneIds)){
                if (sceneIds.contains(",")) {
                    String substring = sceneIds.substring(0, sceneIds.length());
                    String[] result = substring.split(",");
                    StringBuilder stringBuilder = new StringBuilder();
                    for (String s : result) {
                        if (StringUtils.isNotEmpty(s)) {
                            String key = WarehouseTypeEnum.getValueByName(Integer.valueOf(s));
                            stringBuilder.append(key).append(",");
                        }
                    }
                    String key = stringBuilder.deleteCharAt(stringBuilder.length() - 1).toString();
                    map.put("sceneIdStr", key);
                } else {
                    String key = WarehouseTypeEnum.getValueByName(Integer.valueOf(sceneIds));
                    map.put("sceneIdStr", key);
                }
//                map.put("sceneIdStr", ChannelTypeEnum.getValueByName(sceneId));
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
